30. Pressed States on List Item Views
Pressed States on List Item Views
Now, you need to make one last change so that in all the category activities (NumbersActivity, FamilyActivity, etc…), list items show a pressed state when clicked on.
OPTION #1
In the list item layout, you can add a new View that overlaps all other views in the RelativeLayout and matches the RelativeLayout’s width and height. The View has a background of "?android:attr/selectableItemBackground”
, so the View will be transparent by default, so you can see the contents of the list item. Then it will show a pressed state (gray ripple animation on Lollipop devices and above) when the list item is clicked on.
Gist of modified list_item.xml
OPTION #2
Instead of adding a new view to the layout, in the word list layout, you can add the attribute android:drawSelectorOnTop="true"
on the ListView XML element. With this one line change, the pressed state will be shown on each list item.
In word_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true"/>
Either option would work. After you make this change, test your app. Then you’re done with Lesson 4!